<template>
  <div class="login v2">
    <div class="wrapper">
      <div class="dialog dialog-shadow" style="display: block; margin-top: -362px">
        <div class="registered">
          <h4>注册账号</h4>
          <div class="content" style="margin-top: 20px">
            <el-form :model="RegisterForm" :rules="RegisterRules" ref="RegisterForm">
              <el-form-item prop="username">
                <el-input placeholder="用户名" type="text" v-model="RegisterForm.username" autocomplete="off"></el-input>
              </el-form-item>
              <el-form-item prop="password">
                <el-input placeholder="密码" type="password" v-model="RegisterForm.password" autocomplete="off"></el-input>
              </el-form-item>
              <el-form-item prop="rePassword">
                <el-input placeholder="重复密码" type="password" v-model="RegisterForm.rePassword" autocomplete="off"></el-input>
              </el-form-item>
              <el-form-item>
                <el-checkbox class="agree" v-model="agreement">
                  我已阅读并同意遵守
                  <a @click="open('法律声明', '此仅为个人毕业设计，承担不起任何法律问题')">法律声明</a> 和
                  <a @click="open('隐私条款', '本网站将不会严格遵守有关法律法规和本隐私政策所载明的内容收集、使用您的信息')">隐私条款</a>
                </el-checkbox>
              </el-form-item>
              <el-form-item>
                  <el-button type="primary" @click="submitForm()" :disabled="Register">注册</el-button>
              </el-form-item>
            </el-form>
            <div class="border" style="margin-bottom: 10px"></div>
            <ul class="common-form pr">
              <li style="text-align: center; line-height: 48px; margin-bottom: 0; font-size: 12px; color: #999">
                <span>如果您已拥有账号，则可在此</span>
                <a href="javascript:;" style="margin: 0 5px" @click="toLogin()">登陆</a>
              </li>
            </ul>
          </div>
        </div>
      </div>
    </div>
  </div>
</template>
<script>
import { Register } from '../../api/user'

export default {
  name: 'Register',
  data() {
    var validatePass2 = (rule, value, callback) => {
      if (value === '') {
        callback(new Error('请再次输入密码'));
      } else if (value !== this.RegisterForm.password) {
        callback(new Error('两次输入密码不一致!'));
      } else {
        callback();
      }
    };
    var validateInput = (rule, value, callback) => {
      if (!this.checkSpecialKey(value)) {
        callback(new Error("不能含有特殊字符"));
      } else {
        callback();
      }
    };
    return {
      // 注册表单参数
      RegisterForm: {
        username: '',
        password: '',
        rePassword: ''
      },
      // 注册表单验证规则
      RegisterRules: {
        username: [
          { required: true, message: '请输入用户名', trigger: 'blur' },
          { min: 2, max: 16, message: '长度在 2 到 16 个字符', trigger: 'blur' },
          { validator: validateInput, trigger: 'blur' },
        ],
        password: [
          { required: true, message: '请输入密码', trigger: 'blur' },
          { min: 6, max: 12, message: '长度在 6 到 12 个字符', trigger: 'blur' },
          { validator: validateInput, trigger: 'blur' },
        ],
        rePassword: [
          { validator: validatePass2, trigger: 'blur' },
        ]
      },
      // 注册条款须知
      agreement: false,
    }
  },
  computed: {
    // 控制按钮是否可用
    Register() {
      if(this.RegisterForm.username && this.RegisterForm.password && this.RegisterForm.rePassword) {
        return false
      }else {
        return true
      }
    }
  },
  methods: {
    submitForm() {
      this.$refs.RegisterForm.validate(async (valid) => {
        if (!valid) {
          return this.$message.error('请确认相关信息是否填写正确')
        }
        if(!this.agreement){
          return this.$message.error('请阅读并同意相关协议')
        }

        // 注册须知
        const confirmResult = await this.$confirm('请牢记账号密码，丢失后无法找回', '提示', {
          confirmButtonText: '我记牢了',
          cancelButtonText: '我再记忆一下',
          type: 'warning'
        }).catch(err => err)

        if (confirmResult !== 'confirm') {
          return 
        }

        const { data: res } = await Register(this.RegisterForm)

        if(res.code === 10002) {
          return this.$notify.error({
            title: '警告',
            message: '该用户已存在'
          })
        }else if(res.code !== 0) {
          return this.$notify.error({
            title: '警告',
            message: '注册失败'
          })
        }
        
        this.$message({
          message: '恭喜您，注册成功！赶紧登录体验吧',
          type: 'success',
        })
        this.$router.push('/login')
        
      });
    },
    open(t, m) {
      this.$notify.info({
        title: t,
        message: m,
      })
    },
    toLogin() {
      this.$router.push('/login')
    },

    checkSpecialKey(str) {
      let specialKey = "[`~!#$^&*()=|{}':;'\\[\\].<>/?~！#￥……&*（）——|{}【】‘；：”“'。，、？]‘'";
      for (let i = 0; i < str.length; i++) {
        if (specialKey.indexOf(str.substr(i, 1)) != -1) {
          return false;
        }
      }
      return true;
    }
  }
}
</script>
<style lang="scss" rel="stylesheet/scss" scoped>
* {
  box-sizing: content-box;
}

.el-button {
  width: 330px;
}

.el-form-item:nth-child(3) {
  margin-bottom: 12px;
}

.el-form-item:nth-child(4) {
  margin-bottom: 12px;
}

.wrapper {
  background: url(../../assets/img/bg_9b9dcb65ff.png) repeat;
  min-height: calc(100vh);
}

.login {
  overflow-x: hidden;
  overflow-y: hidden;
  .input {
    height: 50px;
    display: flex;
    align-items: center;
    input {
      font-size: 16px;
      width: 100%;
      height: 100%;
      padding: 10px 15px;
      box-sizing: border-box;
      border: 1px solid #ccc;
      border-radius: 6px;
    }
  }
  /* .wrapper {
    background: url(/static/images/bg_9b9dcb65ff.png) repeat;
    background-size: 100px;
    min-height: 800px;
    min-width: 630px;
  } */
}

.v2 .dialog {
  width: 450px;
  border: 1px solid #dadada;
  border-radius: 10px;
  top: 50%;
  left: 50%;
  margin-left: -225px;
  position: absolute;
  .title {
    background: linear-gradient(#fff, #f5f5f5);
    height: auto;
    overflow: visible;
    box-shadow: 0 1px 4px rgba(0, 0, 0, 0.08);
    position: relative;
    background-image: url(/static/images/smartisan_4ada7fecea.png);
    background-size: 140px;
    background-position: top center;
    background-repeat: no-repeat;
    height: 92px;
    margin: 23px 0 50px;
    padding: 75px 0 0;
    box-shadow: none;
    h4 {
      padding: 0;
      text-align: center;
      color: #666;
      border-bottom: 1px solid #dcdcdc;
      -webkit-box-shadow: none;
      -moz-box-shadow: none;
      box-shadow: none;
      font-weight: 700;
      position: absolute;
      bottom: 0;
      width: 100%;
      text-align: center;
      margin: 0;
      padding: 0;
      border-bottom: 0;
      -webkit-box-shadow: none;
      -moz-box-shadow: none;
      box-shadow: none;
      line-height: 1em;
      height: auto;
      color: #333;
      font-weight: 400;
    }
  }
  .content {
    padding: 0 40px 22px;
    height: auto;
    .common-form {
      li {
        clear: both;
        margin-bottom: 15px;
        position: relative;
      }
    }
  }
}

.dialog-shadow,
.v2 .bbs .dialog-shadow,
.v2 .dialog-shadow {
  -webkit-box-shadow: 0 9px 30px -6px rgba(0, 0, 0, 0.2), 0 18px 20px -10px rgba(0, 0, 0, 0.04), 0 18px 20px -10px rgba(0, 0, 0, 0.04), 0 10px 20px -10px rgba(0, 0, 0, 0.04);
  -moz-box-shadow: 0 9px 30px -6px rgba(0, 0, 0, 0.2), 0 18px 20px -10px rgba(0, 0, 0, 0.04), 0 18px 20px -10px rgba(0, 0, 0, 0.04), 0 10px 20px -10px rgba(0, 0, 0, 0.04);
  box-shadow: 0 9px 30px -6px rgba(0, 0, 0, 0.2), 0 18px 20px -10px rgba(0, 0, 0, 0.04), 0 18px 20px -10px rgba(0, 0, 0, 0.04), 0 10px 20px -10px rgba(0, 0, 0, 0.04);
}

@media screen and (min-width: 737px), screen and (-webkit-max-device-pixel-ratio: 1.9) and (max-width: 736px) and (min-device-width: 737px) {
  .dialog {
    background: url(/static/images/dialog-gray-bg.png) #fff bottom repeat-x;
    border-radius: 12px;
    display: none;
    margin: -163px 0 0 -218px;
    width: 436px;
    position: fixed;
    left: 50%;
    top: 50%;
  }
  .dialog .title h4 {
    border-bottom: #d1d1d1 solid 1px;
    box-shadow: 0 2px 6px #d1d1d1;
    color: #666;
    font-size: 20px;
    height: 61px;
    line-height: 61px;
    padding: 0 0 0 35px;
  }
  .common-form li {
    clear: both;
    margin-bottom: 15px;
    position: relative;
  }
  .auto-login {
    position: absolute;
    top: 0px;
    left: 2px;
    color: #999;
  }
  .register {
    padding: 1px 10px 0;
    border-right: 1px solid #ccc;
  }
  .border {
    margin-top: 10px;
    border-bottom: 1px solid #ccc;
  }
  .other {
    margin: 20px 5px 0 0;
    width: auto;
    color: #bbb;
    font-size: 12px;
    cursor: default;
    color: #999;
  }
  .footer {
    display: flex;
    flex-direction: row;
  }
  .agree {
    color: #999;
  }
}

.registered {
  h4 {
    padding: 0;
    text-align: center;
    color: #666;
    border-bottom: 1px solid #dcdcdc;
    -webkit-box-shadow: none;
    -moz-box-shadow: none;
    box-shadow: none;
    font-weight: 700;
    font-size: 20px;
    height: 60px;
    line-height: 60px;
  }
}

#wait {
  text-align: left;
  color: #999;
  margin: 0;
}
</style>
